function [output] = ook_demod (input, interval)
% Function for OOK Demodulation.
% input: the modulated signal
% interval: number of points in one cycle of wave.
% NOTE: length(output)*interval = length(input)

len = (length(input) / interval);
output = zeros(1, len);
c = input(1:interval*interval);
% threshold = sqrt(sum(c.^2))/interval

for j=1:interval
    l1 = (j-1)*interval+1;
    u1 = j*interval;
    d = zeros(1,interval);
    d = input (l1 : u1);
    energy(j) = sqrt(sum(d.^2))/interval;
end
threshold = 0.9*(sum(energy)/interval);
    
for i=1:len
    l1 = (i-1)*interval+1;
    u1 = i*interval;
    d = zeros(1,interval);
    d = input (l1 : u1);
    energy = sqrt(sum(d.^2))/interval;
    %elec(i) = energy;
    if (energy > threshold)
        output(i) = 1;
    end    
end

 output = output(interval+1:end);
 
end